<?php


namespace Plugin\PaypalSiteA\Controllers;

use Beike\Models\Order;
use Beike\Models\ProductSku;
use Beike\Services\StateMachineService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Plugin\PaypalSiteA\Models\PollingSiteBSkus;
use Plugin\PaypalSiteA\Models\PollingSiteIndex;
use Plugin\PaypalSiteA\Models\PollingSiteOrders;
use Plugin\PaypalSiteA\Services\AesTools;
use Plugin\PaypalSiteA\Services\PaypalSiteTools;

class PollingPaypalController
{
    public function query(Request $request)
    {
        $orderNo = $request->order_no;
        $order   = Order::query()->where('number', $orderNo)->first();
        if ($order->status == StateMachineService::PAID) {
            return response()->json([
                'code'   => 0,
                'msg'    => '',
                'status' => StateMachineService::PAID
            ]);
        }
        $paypalSiteTools = new PaypalSiteTools();
        $domain          = $paypalSiteTools->getPollingSetting($orderNo);
        if (empty($domain)) {
            return response()->json([
                'code' => -1,
                'msg'  => '-1'
            ]);
        }
        $token       = null;
        $allSettings = plugin_setting('paypal_site_a' . '.setting', []);
        foreach ($allSettings as $allSetting) {
            if ($allSetting['domain'] == $domain) {
                $token = $allSetting['token'];
                break;
            }
        }
        if (empty($token)) {
            return response()->json([
                'code' => -1,
                'msg'  => '-2'
            ]);
        }
        $data = ['order_no' => $orderNo];
        $rs   = $paypalSiteTools->httpPost($token, $domain, '/callback/paypal_s/query_order', json_encode($data, true));
        if ($rs) {
            $rs = json_decode($rs, true);
        }
        if (isset($rs['code']) && $rs['code'] != 0) {//查询到未支付或其他异常
            return response()->json([
                'code' => -1,
                'msg'  => $rs['msg']
            ]);
        }
        if (isset($rs['status'])) {//查询到未支付或其他异常
            if ($rs['status'] == StateMachineService::PAID) {
                return response()->json([
                    'code'   => 0,
                    'msg'    => '',
                    'status' => $rs['status']
                ]);
            } else {
                return response()->json([
                    'code'   => -1,
                    'msg'    => '',
                    'status' => $rs['status']
                ]);
            }
        }
        return response()->json([
            'code' => -1,
            'msg'  => '查询接口不通'
        ]);

    }


    /**
     * 客户同意后扣款回调
     * @param Request $request
     * @return JsonResponse
     * @throws \Throwable
     */
    public function capture(Request $request)
    {

        $order_no         = $request->order_no;
        $pollingSiteOrder = PollingSiteOrders::query()->where("order_no", $order_no)->where("payment_code", 'paypal_site_a')->first();
        if (empty($pollingSiteOrder)) {
            return '轮询订单不存在';
        }
        $domain      = $pollingSiteOrder->domain;
        $token       = null;
        $allSettings = plugin_setting('paypal_site_a' . '.setting', []);
        foreach ($allSettings as $allSetting) {
            if ($allSetting['domain'] == $domain) {
                $token = $allSetting['token'];
                break;
            }
        }


        $data     = file_get_contents("php://input");
        $data     = json_decode($data, true);
        $time     = $data['time'];
        $key      = md5($time . $token . $time);
        $aesTools = new AesTools($key);
        $data     = $aesTools->decrypt($data['data']);

        try {
            if ($data['status'] == StateMachineService::PAID) {
                $order = Order::query()->where('number', $order_no)->first();
                StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID);
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        return 'success';
    }
}
